package com.example.workrecord.util.dingtalkopenapi.userinfo;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserDeleteRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.response.OapiUserDeleteResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.example.workrecord.exption.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *@ClassName: UserHelper
 *@Description 钉钉人员相关api
 *@Author Mr.Bu
 *@Date 2019/7/24 13:30
 */
public class UserHelper {
    private static final Logger logger=LoggerFactory.getLogger(UserHelper.class);
//    /**
//     * 根据免登授权码查询免登用户userId
//     *
//     * @param accessToken
//     * @param code
//     * @return
//     * @throws Exception
//     */
//    public static  Map<String,Object> getUserInfo(String accessToken, String code) throws Exception {
//        String strResult = HttpUtil.doGet("https://oapi.dingtalk.com/user/getuserinfo?access_token=" + accessToken + "&code=" + code);
//        strResult = new String(strResult.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//        Map<String, Object> result = JsonUtils.getMap(strResult);
//        checkErr(result);
//        return result;
//    }

    public static OapiUserGetResponse getUserInfoDetail(String accessToken, String userId) throws Exception{
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
        OapiUserGetRequest request = new OapiUserGetRequest();
        request.setUserid(userId);
        request.setHttpMethod("GET");
        OapiUserGetResponse response = client.execute(request, accessToken);
        if(!response.isSuccess()){
            logger.error("*************获取用户详情失败*****************失败原因：{}",response);
            throw new BizException("获取用户详情失败:"+response.getErrmsg());
        }
        return response;
    }

//    /**
//     * 创建企业成员
//     * <p>
//     * https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=106816&docType=1#s1
//     */
//    public static String createUser(String accessToken, DingUserDetail userDetail) throws Exception {
//        String url = "https://oapi.dingtalk.com/user/create?access_token=" + accessToken;
//        String strResult = HttpUtil.doPostObj(url, userDetail);
//        Map<String, Object> result = JsonUtils.getMap(strResult);
//        checkErr(result);
//        String userId = result.get("userid").toString();
//        return userId;
//    }
//
//
//    /**
//     * 更新成员
//     * <p>
//     * https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=106816&docType=1#s2
//     */
//    public static void updateUser(String accessToken, XtCorpUserDetail userDetail) throws Exception {
//        String url = "https://oapi.dingtalk.com/user/update?access_token=" + accessToken;
//        HttpUtil.doPostObj(url, userDetail);
//    }


    /**
     * 删除成员
     */
    public static void deleteUser(String accessToken, String userid) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/delete");
        OapiUserDeleteRequest request = new OapiUserDeleteRequest();
        request.setUserid(userid);
        request.setHttpMethod("GET");
        OapiUserDeleteResponse response = client.execute(request, accessToken);
        if(!response.isSuccess()){
            System.out.println("****************删除人员失败！************"+userid);
            throw new Exception("删除人员失败！"+userid);
        }

    }

//
//    //获取成员
//    public static Map<String,Object>  getUser(String accessToken, String userid) throws Exception {
//
//        String url = "https://oapi.dingtalk.com/user/get?access_token=" + accessToken + "&userid=" + userid;
//        String strResult = HttpUtil.doGet(url);
//        strResult = new String(strResult.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//        Map<String, Object> result = JsonUtils.getMap(strResult);
//        checkErr(result);
//        return result;
//    }

//    //批量删除成员
//    public static void batchDeleteUser(String accessToken, List<String> useridlist)
//            throws Exception {
//        CorpUserService corpUserService = ServiceFactory.getInstance().getOpenService(CorpUserService.class);
//        corpUserService.batchdeleteCorpUserListByUserids(accessToken, useridlist);
//
//    }


//    //获取部门成员
//    public static CorpUserList getDepartmentUser(
//            String accessToken,
//            long departmentId,
//            Long offset,
//            Integer size,
//            String order)
//            throws Exception {
//
//        CorpUserService corpUserService = ServiceFactory.getInstance().getOpenService(CorpUserService.class);
//        return corpUserService.getCorpUserSimpleList(accessToken, departmentId,
//                offset, size, order);
//    }
//
//
//    //获取部门成员（详情）
//    public static CorpUserDetailList getUserDetails(
//            String accessToken,
//            long departmentId,
//            Long offset,
//            Integer size,
//            String order)
//            throws Exception {
//
//        CorpUserService corpUserService = ServiceFactory.getInstance().getOpenService(CorpUserService.class);
//        return corpUserService.getCorpUserList(accessToken, departmentId,
//                offset, size, order);
//    }
//
//    public static void checkErr(Map result) {
//        if (result.get("errcode") != null && !"0".equals(result.get("errcode").toString())) {
//            throw new BizException(result.get("errmsg").toString());
//        }
//    }
//
//
//    public static void createDingUser(String accessToken, String extrr, String order,List<Long> dept, UnifiedUser user) throws Exception{
//        OapiUserCreateRequest request = new OapiUserCreateRequest();
//        request.setUserid(user.getIdmEmployeeId());
//        request.setName(user.getUserName());
//        request.setMobile(user.getMobilePhone());
//        request.setWorkPlace(user.getOfficePlace());
//        request.setPosition(user.getPositionDesc());
//        request.setEmail(user.getMailbox());
//        request.setOrgEmail(user.getMailbox());
//        request.setJobnumber(user.getHrEmployeeId());
//        request.setExtattr(extrr);
//        request.setOrderInDepts(order);
//        request.setDepartment(JSON.toJSONString(dept));
//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/create");
//        OapiUserCreateResponse response = client.execute(request, accessToken);
//        if(!response.isSuccess()){
//            System.out.println("**************人员创建失败*****************"+response.getErrmsg());
//            throw new BizException("钉钉人员创建失败："+response.getErrmsg());
//        }
//
//    }
//
//    public static void updateDingUser(String accessToken, String extrr, String order, List<Long> dept, UnifiedUser user) throws Exception{
//        OapiUserUpdateRequest request = new OapiUserUpdateRequest();
//        request.setUserid(user.getIdmEmployeeId());
//        request.setName(user.getUserName());
//        request.setMobile(user.getMobilePhone());
//        request.setWorkPlace(user.getOfficePlace());
//        request.setPosition(user.getPositionDesc());
//        request.setEmail(user.getMailbox());
//        request.setOrgEmail(user.getMailbox());
//        request.setJobnumber(user.getHrEmployeeId());
//        request.setExtattr(extrr);
//        request.setOrderInDepts(order);
//        request.setDepartment(dept);
//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/update");
//        OapiUserUpdateResponse response = client.execute(request, accessToken);
//        if(!response.isSuccess()){
//            System.out.println("**************人员跟新失败*****************"+response.getErrmsg());
//            throw new BizException("钉钉人员跟新失败："+response.getErrmsg());
//        }
//
//    }
}
